package com.service.finance;

import com.common.Common;
import com.common.PageUtils;
import com.dao.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.*;
import com.query.PlatformFlowQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @Description:
 * @date 2021/4/17 12:02
 */
@Slf4j
@Service
public class FinanceService {

    @Autowired
    private BlacklistDao blacklistDao;
    @Autowired
    private CustomerDao customerDao;
    @Autowired
    private CustomerAuthDao customerAuthDao;
    @Autowired
    private TransferRecdDao transferRecdDao;
    @Autowired
    private CoinAddrDao coinAddrDao;
    @Autowired
    private DrawithRecdDao drawithRecdDao;
    @Autowired
    private ExtractTicketDao extractTicketDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysOperateLogDao sysOperateLogDao;


    /**
     * 平台流水
     * @param query
     */
    public PageInfo<TransferRecdModel> platformFlow(PlatformFlowQuery query){
        PageHelper.startPage(PageUtils.initPage(query.getPageNo()), query.getPageSize());
        List<TransferRecdModel> list = transferRecdDao.platformFlow(query);
        for (TransferRecdModel transferRecdModel : list) {
            //0收入，1支出，2冻结，3解冻
            inOutName(transferRecdModel);
            //0 待处理，1成功，2失败,3 审核成功， 4审核失败
            statusName(transferRecdModel);
            //0站内，1站外
            transferRecdModel.setTransTypeName(transferRecdModel.getTransType() == 0?"站内":"站外");
        }
        return new PageInfo<>(list);
    }

    private void statusName(TransferRecdModel transferRecdModel){
        Integer status = transferRecdModel.getStatus();
        switch (status){
            case 0:
                transferRecdModel.setStatusName("待处理");
                break;
            case 1:
                transferRecdModel.setStatusName("成功");
                break;
            case 2:
                transferRecdModel.setStatusName("失败");
                break;
            case 3:
                transferRecdModel.setStatusName("审核成功");
                break;
            case 4:
                transferRecdModel.setStatusName("审核失败");
                break;
        }
    }

    private void inOutName(TransferRecdModel transferRecdModel){
        Integer inOut = transferRecdModel.getInOut();
        switch (inOut){
            case 0:
                transferRecdModel.setInOutName("收入");
                break;
            case 1:
                transferRecdModel.setInOutName("支出");
                break;
            case 2:
                transferRecdModel.setInOutName("冻结");
                break;
            case 3:
                transferRecdModel.setInOutName("解冻");
                break;
        }
    }

    /**
     * 提币流水
     * @param name
     * @param coinSymbol
     * @param pageNo
     * @param pageSize
     */
    public PageInfo<DrawithRecdModel> withdrawFlowList(String name, String coinSymbol,String pageNo,Integer pageSize){
        PageHelper.startPage(PageUtils.initPage(pageNo), pageSize);
        List<DrawithRecdModel> list = drawithRecdDao.drawithRecdList(name, coinSymbol);
        for (DrawithRecdModel drawithRecdModel : list) {
            //0成功，1不成功
            drawithRecdModel.setStateName(drawithRecdModel.getState() == 0?"成功":"不成功");
        }
        return new PageInfo<>(list);
    }

    /**
     * 提币审核列表
     * @param name
     * @param coinSymbol
     * @param status
     */
    public PageInfo<DrawithRecdModel> withdrawReviewList(String name, String coinSymbol,Integer status,String pageNo,Integer pageSize){
        PageHelper.startPage(PageUtils.initPage(pageNo), pageSize);
        List<DrawithRecdModel> list = drawithRecdDao.withdrawReviewList(name, coinSymbol,status);
        for (DrawithRecdModel drawithRecdModel : list) {
            //0成功，1不成功
            drawithRecdModel.setStateName(drawithRecdModel.getState() == 0?"成功":"不成功");
        }
        return new PageInfo<>(list);
    }

    /**
     * 提币审核
     * @param id
     * @param status
     */
    public void withdrawReviewOperate(Long id, Integer status){
        drawithRecdDao.withdrawReviewOperate(id,status);
    }


    /**
     * 提币券管理列表
     * @param name
     */
    public PageInfo<ExtractTicketModel> withdrawRollList(String name,String pageNo,Integer pageSize){
        PageHelper.startPage(PageUtils.initPage(pageNo), pageSize);
        List<ExtractTicketModel> list = extractTicketDao.list(name);
        return new PageInfo<>(list);
    }

    /**
     * 提币券管理新增
     * @param customerId
     * @param num
     */
    @Transactional
    public void withdrawRollAdd(Long customerId,Integer num,Long userId,String username) throws Exception{
        Customer customer = customerDao.getById(customerId);
        if(customer == null){
            throw new Exception("用户不存在");
        }
        ExtractTicket byUserId = extractTicketDao.getByUserId(customerId);
        if(byUserId == null){
            //新增
            extractTicketDao.addExtractTicket(customerId,num);
        }else{
            extractTicketDao.updateExtractTicket(byUserId.getId(),byUserId.getTicketNumber()+num);
        }
        //新增明细
        extractTicketDao.addExtractTicketRed(customerId,num,byUserId==null?num:byUserId.getTicketNumber(),0,userId);

        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(userId);
        sysOperateLog.setUserName(username);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_ADD);
        sysOperateLog.setDescription("赠送提币券");
        sysOperateLog.setDataCcope("赠送提币券");
        sysOperateLogDao.insert(sysOperateLog);
    }

    /**
     * 提币券管理删除
     * @param id
     */
    public void withdrawRollDel(Long id){

    }

    /**
     * 黑名单列表
     * @param name
     */
    public PageInfo<BlacklistModel> blacklist(String name,String pageNo,Integer pageSize){
        PageHelper.startPage(PageUtils.initPage(pageNo), pageSize);
        List<BlacklistModel> list = blacklistDao.list(name);
        return new PageInfo<>(list);
    }

    /**
     * 黑名单新增
     * @param userIdDel
     */
    public void blacklistAdd(Long userIdDel,Integer type,String userId, @RequestHeader String username) throws Exception{
        Customer customer = customerDao.getById(userIdDel);
        if(customer == null){
            throw new Exception("未获取到用户信息");
        }
        Blacklist byUserId = blacklistDao.getByUserId(userIdDel);
        if(byUserId != null){
            throw new Exception("该用户已加入黑名单");
        }
        Blacklist blacklist = new Blacklist();
        blacklist.setUserId(userIdDel);
        blacklist.setType(type);
        blacklist.setCreateTime(new Date());
        blacklist.setMobile(customer.getMobile());
        blacklist.setState(0);//TODO 待定
        blacklistDao.add(blacklist);

        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.valueOf(userId));
        sysOperateLog.setUserName(username);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_ADD);
        sysOperateLog.setDescription("新增黑名单");
        sysOperateLog.setDataCcope("新增黑名单");
        sysOperateLogDao.insert(sysOperateLog);
    }

    /**
     * 黑名单移除
     * @param id
     */
    public void blacklistDel(Long id,String userId, String userName){
        blacklistDao.del(id);
        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.valueOf(userId));
        sysOperateLog.setUserName(userName);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_DELETE);
        sysOperateLog.setDescription("移除黑名单");
        sysOperateLog.setDataCcope("移除黑名单");
        sysOperateLogDao.insert(sysOperateLog);
    }

    /**
     * 账号统计
     * @param name
     * @param coinSymbol
     */
    public PageInfo<CoinAddrModel> accountList(String name, String coinSymbol,String pageNo,Integer pageSize){
        PageHelper.startPage(PageUtils.initPage(pageNo), pageSize);
        List<CoinAddrModel> coinAddrModels = coinAddrDao.accountList(name, coinSymbol);
        return new PageInfo<>(coinAddrModels);
    }

    /**
     * 实名认证列表
     * @param name
     * @param number
     * @param status
     */
    public PageInfo<CustomerAuthModel> verifiedList(String name, String number,Integer status,String pageNo,Integer pageSize){
        PageHelper.startPage(PageUtils.initPage(pageNo), pageSize);
        List<CustomerAuthModel> customerAuthModels = customerAuthDao.verifiedList(name, number, status);
        return new PageInfo<>(customerAuthModels);
    }

    /**
     * 实名认证审核
     * @param id
     * @param status
     */
    public void verifiedOperate(Long id, Integer status,String userId, String userName){
        customerAuthDao.verifiedOperate(id,status);
        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.valueOf(userId));
        sysOperateLog.setUserName(userName);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_UPDATE);
        String description = status==1?"通过":"不通过";
        sysOperateLog.setDescription("实名认证审核-"+description);
        sysOperateLog.setDataCcope("实名认证审核-"+description);
        sysOperateLogDao.insert(sysOperateLog);
    }

}
